// 服务
//get
//post
app.factory('httpService', ['$http', '$rootScope', '$localStorage', '$location', 'toaster'
    , function($http, $rootScope, $localStorage, $location, toaster){

        var doRequest = function(type, path, params, success_callback, error_callback, isNeedLoad){
            var url;
            if(path=="qiniu"){
                if(params){
                    url=CONFIG.qiniu_tokenPath+"?key="+params;
                }else{
                    url=CONFIG.qiniu_tokenPath;
                }
            }else{
                url=CONFIG.basePath + path;
            }
            if(path.indexOf("?") >= 0){
                path += '&random=' + Math.random();
            } else {
                path += '?random=' + Math.random();
            }
            /*if(isNeedLoad == undefined){
             isNeedLoad = true; //是否显示加载中 图标
             }
             if(!$rootScope.isLoading && isNeedLoad){
             //开启加载图标
             $rootScope.isLoading = true;
             }*/
            return $http({
                method : type,
                data : params,
                url : url,
                headers : {'Content-Type' : 'application/json'},
            }).success(
                function(data){
                    if(data.success){
                        if(success_callback){
                            success_callback(data);
                        }
                    }else{
                        if(error_callback){
                            error_callback(data.message);
                        }else{
                            toaster.error("错误", data.message);
                        }
                    }
                }
            ).error(function(error){
                //关闭加载图标
                /*$rootScope.isLoading = false;
                 */
                if(error_callback){
                    error_callback(error);
                } else {
                    toaster.error("错误", error.Message);
                }
            });
        }
        return {
            request : function(type, path, params, success_callback, error_callback, isNeedLoad){
                return doRequest(type, path, params, success_callback, error_callback, isNeedLoad)
            }
        };
    }
]);

//http 拦截器
app.factory('HttpInterceptor', ['$q', '$localStorage', '$location', '$timeout',
    function($q, $localStorage, $location, $timeout){

        return {
            // 请求发出之前，可以用于添加各种身份验证信息
            request : function(config){

                if($localStorage.token){
                    config.headers.Authorization = 'bearer ' + $localStorage.token;
                }
                return config;
            },
            // 请求发出时出错
            requestError : function(err){
                return $q.reject(err);
            },
            // 成功返回了响应
            response : function(res){
                return res;
            },
            // 返回的响应出错，包括后端返回响应时，设置了非 200 的 http 状态码
            responseError : function(err){
                err.Message = "";
                if(err.status === 401){
                    console.log(" 401无权限 ，进行跳转至登录界面 ");
                    err.Message = " 401无权限 ，进行跳转至登录界面 ";
                    $timeout(function(){
                        $location.path('/login');
                    }, 2000);
                } else if(err.status === 500){
                    console.log(" 500 ，服务器错误，请联系管理员！ ");
                    err.Message = " 500 ，服务器错误，请联系管理员！ ";
                }
                return $q.reject(err);
            }
        };

    }]);
//获取qinniu的token
app.service('QiniuToken',function(httpService,$localStorage){
    this.getToken = function(callBack) {
        httpService.request("get","qiniu",null,function(result){
            $localStorage.qiniuToken = result.data.Token;
            $localStorage.Domain = result.data.Domain+"/";
            if(callBack){
                callBack();
            }
        });
    };
    this.delImg=function(key,callBack){
        var url="";
        httpService.request("get","qiniu",key,function(result){
            if(callBack){
                callBack();
            }
        });
    }
});

